# coding=utf-8

import web

"""
define.py

对项目中使用的选项常量值进行定义
"""
def convert_dict(puple, index):
    """
    将给定元组以指定位置处的项为key的词典
    """
    return {item[index]:item for item in puple}

TRAVEL_TYPES = (
        ('0', '', ()),
        ('1', u'酒店', ('hotel',)),
        ('2', u'旅游线路', ('group',)),
        ('3', u'航班', ('flight',))
    )

def travel_type_alias(key, index = 0):
    """
    获取给定的类型的指定英文别名
    @key as int, 类型的数字码
    @index as int, 要获取的别名序号，默认为第一个
    """
    if isinstance(key, (int, long)):
        key = str(key)
    
    for k,zv,evs in TRAVEL_TYPES:
        if k == key:
            return evs[min(index, len(evs))]
    return 'None'

web.template.Template.globals['travel_type_alias'] = travel_type_alias

def travel_type_key(alias):
    """
    将给定的type text 转换为数字id
    @alias as string, 英文别名
    """
    if not alias:
        return 0

    for k,zv,evs in TRAVEL_TYPES:
        if alias in evs:
            return k

    return 0

def travel_type_text(key):
    """
    将指定的type key 转换为中文描述
    """
    if not isinstance(key, str):
        key = str(key)

    for t in TRAVEL_TYPES:
        if key == t[0]:
            return t[1]

    return ''

web.template.Template.globals['travel_type_text'] = travel_type_text

DEST_TYPES = (
        ('0', ''),
        ('1', u'境内游'),
        ('2', u'境外游'),
        ('3', u'周边游'),
    )

def dest_type_text(key):
    """
    将指定的 dest key 转换为中文描述
    """
    if not isinstance(key, str):
        key = str(key)

    for t in DEST_TYPES:
        if key == t[0]:
            return t[1]

    return ''

web.template.Template.globals['dest_type_text'] = dest_type_text

OFTEN_DEPARTS = (
        (0, u'', None, None),
        (10, u'北京', 'beijing', None,),
        (21, u'上海', 'shanghai', None,),
        (992, u'广州', 'guangzhou', None,),
        (23, u'重庆', 'chongqing', None,),
        (993, u'深圳', 'shenzhen', None,),
        (994, u'成都', 'chengdu', None,),
        (995, u'南京', 'nanjing', None,),
        (996, u'杭州', 'hangzhou', None,),
        (997, u'武汉', 'wuhan', None,),
        (998, u'西安', 'xian', None,),
        (999, u'厦门', 'xiamen', None,),
        (1000, u'福州', 'fuzhou', None,),
        (1001, u'沈阳', 'shenyang', None,),
        (1002, u'郑州', 'zhengzhou', None,),
        (1003, u'昆明', 'kunming', None,),
        (1004, u'香港', 'hongkong', None,),
        (1005, u'澳门', 'macao', None,),
        (1006, u'长沙', 'changsha', None,),
        (1007, u'苏州', 'suzhou', None,),
        (1008, u'无锡', 'wuxi', None,),
        (1009, u'南宁', 'nanning', None,),
        (1010, u'桂林', 'guilin', None,),
        (1011, u'海口', 'haikou', None,),
        (1012, u'三亚', 'sanya', None,),
        (1013, u'哈尔滨', 'haerbin', None,),
        (65535, u'其他', 'other', (
                    0,993,994,995,996,997,998,999,1000,
                    1001,1002,1003,1004,1005,1006,1007,
                    1008,1009,1010,1011,1012,1013,)
                ),
    )

# 以别名做key的常用出发城市词典
OFTEN_DEPARTS_ALIAS = convert_dict(OFTEN_DEPARTS, 2)
# 以内码作key的常用胡出发城市词典
OFTEN_DEPARTS_KEY = convert_dict(OFTEN_DEPARTS, 0)

def depart_id_by_alias(alias):
    """
    通过别名查找出发城市的内码
    @alias as str, 别名
    """
    if not (alias in OFTEN_DEPARTS_ALIAS):
        return 0

    return OFTEN_DEPARTS_ALIAS[alias][0]


def depart_name_by_id(key):
    """
    获取给定出发城市得名字，通过内码
    @key as int, 内码
    """
    if not (key and key in OFTEN_DEPARTS_KEY):
        return None
    
    return OFTEN_DEPARTS_KEY[key][1]


def depart_alias_by_id(key):
    """
    获取给定出发城市的别名，通过内码
    @key as int, 内码
    """
    if not (key and key in OFTEN_DEPARTS_KEY):
        return ''

    return OFTEN_DEPARTS_KEY[key][2]

def depart_by_alias(alias):
    """
    获取给定出发城市别名所代表的城市内码（可能是多个）
    """
    if not (alias and alias in OFTEN_DEPARTS_ALIAS):
        return None

    return OFTEN_DEPARTS_ALIAS[alias]


web.template.Template.globals['depart_name_by_id'] = depart_name_by_id
web.template.Template.globals['depart_alias_by_id'] = depart_alias_by_id

